﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using TR.AppService.Dto.RKGLDto;
using TR.Domain.Entity.CKGL;
using TR.Domain.Entity.RK;
using TR.Domain.Repository.CKGLRepository;

namespace TR.EFCore.Repository.CKGLRepository
{
    public class wh_Outbill_hRepository : Iwh_Outbill_hRepository
    {
        private readonly MyContext context;

        private int glzts;

        public wh_Outbill_hRepository(MyContext context)
        {
            this.context = context;
        }

        //出库管理新增
        public async Task<bool> CKGLInsert(wh_Outbill_h jcxx, List<wh_Outbill_d> ckmx)
        {
            var query = context;
            try
            {
                var wihadd = new wh_Outbill_h()
                {
                    outbill_code = jcxx.outbill_code,
                    outbill_type = jcxx.outbill_type,
                    outbill_date = jcxx.outbill_date,
                    sale_code = jcxx.sale_code,
                    cust_code = jcxx.cust_code,
                    link_man = jcxx.ware_code,
                    link_way = jcxx.link_way,
                    duty_man = jcxx.duty_man,
                    ware_code = jcxx.ware_code,
                    create_by = jcxx.create_by,
                    create_time = jcxx.create_time,
                    sure_state = '2',
                    remark = jcxx.remark,
                    is_del = 0,
                };
                query.wh_Outbill_h.Add(wihadd);

                foreach (var xq in ckmx)
                {
                    var wids = new wh_Outbill_d()
                    {
                        item_code = xq.item_code,
                        outbill_code = jcxx.outbill_code,
                        item_type = xq.item_type,
                        unit = xq.unit,
                        sale_num = xq.sale_num,
                        outbill_num = xq.outbill_num,
                        area_code = xq.area_code,
                        sale_price = xq.sale_price,
                        product_date = xq.product_date,
                        batch_no = xq.batch_no,
                        create_by = xq.create_by,
                        create_time = xq.create_time,
                        is_del = 0,
                    };
                    query.wh_Outbill_d.Add(wids);
                }
                await query.SaveChangesAsync();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("错误信息:" + ex.Message);
                return false;
            }
        }

        //出库关联关联单号
        public async Task<List<CKDHGuanlian>> CKGLGuanlian(int mys, int dqy, string? dqsj, string? jssj, CKGLDHTjDto tj)
        {
            var query = context.CKDHGuanlian.AsQueryable();

            query = query.Where(p => p.sure_state == '4');

            //根据时间查询
            if (!string.IsNullOrEmpty(dqsj) && !string.IsNullOrEmpty(jssj))
            {
                query = query.Where(p => string.Compare(p.sale_date, dqsj) >= 0 && string.Compare(p.sale_date, jssj) <= 0);
            }

            //根据实体类条件查询
            if (tj != null)
            {
                if (!string.IsNullOrEmpty(tj.sale_code))
                {
                    query = query.Where(p => EF.Functions.Like(p.sale_code, "%" + tj.sale_code + "%"));
                }
                if (!string.IsNullOrEmpty(tj.cust_code))
                {
                    query = query.Where(p => p.cust_code == tj.cust_code);
                }
                if (!string.IsNullOrEmpty(tj.sale_duty_man))
                {
                    query = query.Where(p => p.sale_duty_man == tj.sale_duty_man);
                }
            }
            var result = await query.OrderByDescending(p => p.sale_date).Skip(dqy * (mys - 1)).Take(dqy).ToListAsync();//异步使用ToListAsync

            glzts = await query.CountAsync();

            return result;
        }

        public async Task<int> Ztshu()
        {
            return glzts;
        }

        //根据销售单号查对应货品
        public async Task<List<XSDHchaHP>> XSDHChahp(string sale_code, HPTj tj)
        {
            var query = context.XSDHchaHP.AsQueryable();

            query = query.Where(p => p.sale_code == sale_code);

            if (tj != null)
            {
                if (!string.IsNullOrEmpty(tj.item_code))
                {
                    query = query.Where(p => EF.Functions.Like(p.item_code, "%" + tj.item_code + "%"));
                }
                if (!string.IsNullOrEmpty(tj.unit))
                {
                    query = query.Where(p => p.unit == tj.unit);
                }
            }
            var result = query.ToListAsync();

            return await result;
        }

        //出库管理主表分页查询
        public async Task<List<CKGL_SelectZB>> CKGLZBSelect(int mys, int dqy, CKGLZbTjCx tj)
        {
            var query = context.CKGL_SelectZB.AsQueryable();

            query = query.Where(p => p.sure_state == '4');

            //根据实体类条件查询
            if (tj != null)
            {
                if (!string.IsNullOrEmpty(tj.outbill_code))
                {
                    query = query.Where(p => EF.Functions.Like(p.outbill_code, "%" + tj.outbill_code + "%"));
                }
                if (!string.IsNullOrEmpty(tj.outbill_type))
                {
                    query = query.Where(p => p.outbill_type == tj.outbill_type);
                }
                if (tj.sure_state != '\0')
                {
                    query = query.Where(p => p.sure_state == tj.sure_state);
                }
                if (!string.IsNullOrEmpty(tj.ware_name))
                {
                    query = query.Where(p => p.ware_name == tj.ware_name);
                }
            }
            var result = await query.OrderByDescending(p => p.outbill_date).Skip(dqy * (mys - 1)).Take(dqy).ToListAsync();//异步使用ToListAsync

            glzts = await query.CountAsync();

            return result;
        }

        //出库订单总条数
        public Task<int> ZbZts()
        {
            throw new NotImplementedException();
        }

        //出库管理
        public async Task<bool> CKGLDelete(List<int> isdel)
        {
            try
            {
                var query = context;

                var ckgldel = query.wh_Outbill_h.Where(p => isdel.Contains(p.id)).ToList();

                foreach (var id in ckgldel)
                {
                    id.is_del = 1;
                    query.Entry(id).State = EntityState.Modified;
                }
                query.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("错误");
                return false;
            }
        }
    }
}
